package com.fly.demo.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fly.common.BaseController;
import com.fly.common.result.JsonResult;
import com.fly.demo.entity.User;
import com.fly.demo.service.UserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 00fly
 * @since 2018-09-11
 */
@Controller
@RequestMapping("/demo/user")
public class UserController extends BaseController
{
    @Autowired
    DataSource datasource;
    
    @Autowired
    private UserService userservice;
    
    @RequestMapping({"/", ""})
    public String index(ModelMap model)
    {
        model.addAttribute("data", userservice.selectList(null));
        return "user/index";
    }
    
    @RequestMapping("/build")
    public String build(ModelMap model)
    {
        // 重建表
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        jdbcTemplate.execute("DROP TABLE IF EXISTS `user`");
        jdbcTemplate.execute(
            "CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, `enabled` bit(1) DEFAULT NULL, `create_date` datetime DEFAULT NULL, `age` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) )");
        
        // 插入数据
        List<User> users = new ArrayList<>();
        Date now = new Date();
        for (int i = 0; i < 10; i++)
        {
            User user = new User();
            user.setUsername("username_" + RandomStringUtils.randomAlphabetic(5));
            user.setPassword("password_" + RandomStringUtils.randomAlphabetic(5));
            user.setName("name_" + RandomStringUtils.randomAlphabetic(5));
            user.setCreateDate(now);
            user.setAge(RandomUtils.nextInt(10, 30));
            user.setEnabled(true);
            users.add(user);
        }
        userservice.insertBatch(users);
        model.addAttribute("data", userservice.selectList(null));
        return "user/index";
    }
    
    @RequestMapping("/preSave")
    public String preSave(ModelMap model, @RequestParam(value = "id", required = false) Long id)
    {
        if (id != null)
        {
            model.addAttribute("data", userservice.selectById(id));
        }
        return "user/save";
    }
    
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(User entity)
    {
        if (entity.getId() == null)
        {
            return userservice.insert(entity) ? renderSuccess("添加成功") : renderError("添加失败");
        }
        else
        {
            return userservice.updateById(entity) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }
    
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) Long id)
    {
        return userservice.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }
    
    @ResponseBody
    @RequestMapping("/testTrans")
    public JsonResult testTrans()
    {
        userservice.testTrans();
        return renderSuccess("运行成功");
    }
}
